package com.ruoyi.web.controller.project;

import com.dgy.common.constants.ProjectConstants;
import com.dgy.project.domain.ProjectApply;
import com.dgy.project.domain.ProjectMember;
import com.dgy.project.service.IProjectApplyService;
import com.dgy.project.service.IProjectMemberService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.dgy.project.domain.ProjectInfo;
import com.dgy.project.service.IProjectInfoService;

/**
 * 课题申报信息Controller
 *
 * @author ccc
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/project/info")
public class ProjectInfoController extends BaseController
{
    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectMemberService projectMemberService;

    @Autowired
    private IProjectApplyService projectApplyService;

    /**
     * 获取课题申报信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectInfoService.selectProjectInfoById(id));
    }

    /**
     * 新增课题申报信息
     */
    @PreAuthorize("@ss.hasPermi('project:info:add')")
    @Log(title = "课题申报信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody ProjectInfo projectInfo)
    {
        // 新增课题基本信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectInfo.setCreateBy(loginUser.getUsername());
        projectInfoService.insertProjectInfo(projectInfo);
        // 项目组成员信息中增加负责人信息
        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(projectInfo.getProjectId());
        projectMember.setUserId(projectInfo.getLeaderUserId());
        projectMember.setName(projectInfo.getLeaderName());
        projectMember.setDeptId(projectInfo.getDeptId());
        projectMember.setDeptName(projectInfo.getDeptName());
        projectMember.setLeaderFlag(ProjectConstants.FLAG_YES);
        projectMemberService.insertProjectMember(projectMember);
        // 初始化申报阶段的申请（申请状态为待审核）
        ProjectApply nextApply = new ProjectApply();
        nextApply.setProjectId(projectInfo.getProjectId());
        nextApply.setApplyStep(ProjectConstants.APPLY_STEP);
        nextApply.setApplyStatus(ProjectConstants.AUDIT_STATUS_UN_SUBMIT);
        projectApplyService.insertProjectApply(nextApply);
        return success(projectInfo.getProjectId());
    }

    /**
     * 修改课题申报信息
     */
    @PreAuthorize("@ss.hasPermi('project:info:edit')")
    @Log(title = "课题申报信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@RequestBody ProjectInfo projectInfo)
    {
        // 判断当前课题信息是否允许修改
        ProjectInfo oldProjectInfo = projectInfoService.selectProjectInfoById(projectInfo.getProjectId());
        if(!ProjectConstants.PROJECT_INFO_STATUS_UN_SUBMIT.equals(oldProjectInfo.getProjectStatus())){
            return error("课题当前状态不允许修改");
        }
        // 修改课题基本信息
        projectInfoService.updateProjectInfo(projectInfo);
        //  若更改了负责人信息，变更成员列表中的负责人成员
        if(!oldProjectInfo.getLeaderUserId().equals(projectInfo.getLeaderUserId())){
            ProjectMember projectLeaderMember = new ProjectMember();
            projectLeaderMember.setProjectId(projectInfo.getProjectId());
            projectLeaderMember.setUserId(projectInfo.getLeaderUserId());
            projectLeaderMember.setName(projectInfo.getLeaderName());
            projectLeaderMember.setDeptId(projectInfo.getDeptId());
            projectLeaderMember.setDeptName(projectInfo.getDeptName());
            projectMemberService.changeProjectLeader(projectLeaderMember);
        }
        return toAjax(true);
    }

}
